package com.example.Lezedata.ui.activity.award;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.Lezedata.R;
import com.example.Lezedata.dataManager.LoginManager;
import com.example.Lezedata.dataModel.GoodsProjectModel;
import com.example.Lezedata.net.GBError;
import com.example.Lezedata.net.GBNetRequest;
import com.example.Lezedata.net.ServerResponseObserver;
import com.example.Lezedata.ui.activity.address.Address_addVc;
import com.example.Lezedata.ui.activity.baseClass.GBBaseVc;
import com.example.Lezedata.ui.activity.common.TransitionPage;
import com.example.Lezedata.ui.activity.goods.GoodsNameView;
import com.example.Lezedata.ui.activity.person.PersonVc;
import com.example.Lezedata.ui.fragment.commonfragment.AlertFragment;
import com.example.Lezedata.util.GBToast;

import bid.Bid;

public class ReceiveAwardVc extends GBBaseVc {

    @Override
    protected int getLayoutId() {
        return R.layout.activity_receive_award_vc;
    }

    @Override
    public CharSequence title() {
        return "Receive award";
    }

    private TextView addAddress;
    private LinearLayout contentView;
    private TextView submit;
    private TextView change;
    private TextView address;

    TextView tx_time;
    ImageView tx_imageView;
    TextView tx_code;
    GoodsNameView tx_goodsNameView;

    private String addressId;
    @Override
    protected void viewDidLoad() {
        super.viewDidLoad();
        findSec1View();
        findView();
    }

    void findView(){
        addAddress = findViewByTag("add");
        contentView = findViewByTag("layout");
        submit = findViewByTag("submit");
        change = findViewByTag("change");
        address = findViewByTag("address");
        contentView.removeView(address);
        addAction();
    }

    void findSec1View(){
        GoodsProjectModel projectModel = GoodsProjectModel.testGoodsProjectModel();
        tx_time = findViewById(R.id.tx_time);
        tx_imageView = findViewById(R.id.tx_imageView);
        tx_code = findViewById(R.id.tx_code);
        tx_goodsNameView = findViewById(R.id.tx_goodsNameView);
        tx_goodsNameView.setModelForNameWithDetail(projectModel.infoModel.goodsName,projectModel.infoModel.goodsDetail);
        Glide.with(tx_imageView).load(projectModel.infoModel.goodsIcon).into(tx_imageView);
    }

    void addAction(){
        addAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext,Address_addVc.class);
                jumpActivityIntent(intent,1);
            }
        });

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submit();
            }
        });

        change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                change();
            }
        });
    }

    void addAddressComplete(){
        contentView.removeView(addAddress);
        contentView.addView(address);
        String str = String.format("Name:XXXXXXXX\nPhone:08334448880\nAddress:8 Sawyerr Street, Dopemu Agege,Lagos");
        address.setText(str);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case 1:
                addAddressComplete();
                break;
        }
    }

    void change(){
        AlertFragment fragment = new AlertFragment();
        fragment.text = "Are you sure you want to convert into balance";
        fragment.detail = "Exchange the prize with 80% of the prize value into the balance";
        fragment.okListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeSuccess();
            }
        };
        fragment.show(getSupportFragmentManager(),"alert");
    }

    void submit(){
        if (addressId.isEmpty()){
            GBToast.shortToast("Please select a shipping address");
            return;
        }
        Bid.RedeemGoodsRequest request = Bid.RedeemGoodsRequest.newBuilder()
                .setDrawBidJournalId(getBid())
                .setExpressAddressId(addressId)
                .setRedeemGoodsCategory(Bid.RedeemGoodsCategory.RGC_GOODS)
                .build();
        GBNetRequest.stubUser().redeemGoods(request, ServerResponseObserver.observer(new ServerResponseObserver.requestListener() {
            @Override
            public void complete(Object value, GBError error) {
                if (error == null){
                    redeemGoodsFinish();
                }
            }
        }));
    }

    void redeemGoodsFinish(){
        Intent intent = new Intent(mContext,TransitionPage.class);
        intent.putExtra(TransitionPage.Key_title,"Successful!");
        intent.putExtra(TransitionPage.Key_tip,"Your application has been submitted, the prize will be issued within 7 days, please be patient.");
        intent.putExtra(TransitionPage.Key_image,R.drawable.lottery_wait_max);
        jumpActivityIntent(intent);
    }

    void changeSuccess(){
        Bid.RedeemCoinRequest request = Bid.RedeemCoinRequest.newBuilder().
                setDrawBidJournalId(getBid()).build();
        GBNetRequest.stubUser().redeemCoin(request, ServerResponseObserver.observer(new ServerResponseObserver.requestListener() {
            @Override
            public void complete(Object value, GBError error) {
                if (error == null){
                    redeemCoinFinish();
                }
            }
        }));
    }

    void redeemCoinFinish(){
        Intent intent = new Intent(mContext,TransitionPage.class);
        intent.putExtra(TransitionPage.Key_title,"Successful!");
        intent.putExtra(TransitionPage.Key_tip,"Bonus has entered your balance.");
        intent.putExtra(TransitionPage.Key_image,R.drawable.lottery_wait_max);
        jumpActivityIntent(intent);
    }

    // 获取id
    String getBid(){
        String bid = getIntent().getStringExtra("goodsId");
        if (bid == null){
            bid = "";
        }
        return bid;
    }
}